import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import dts from 'vite-plugin-dts'

import { resolve } from 'path'

export default defineConfig({
  resolve: {
    alias: [{ find: '@', replacement: resolve(__dirname, './packages') }],
  },
  plugins: [
    vue(),
    dts({
      outDir: ['./dist/es/packages', './dist/lib/packages'],
      include: ['packages/**/*.{vue,ts}'],
      exclude: ['packages/styles'],
      tsconfigPath: './tsconfig.app.json',
    }),
  ],
  // 打包配置
  build: {
    // 打包后的文件输出目录
    outDir: 'dist',
    lib: {
      //指定组件编译入口文件
      entry: './packages/index.ts',
    },

    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: ['vue'],
      input: ['./packages/index.ts'],
      output: [
        {
          //打包格式
          format: 'es',
          //打包后文件名
          entryFileNames: '[name].mjs',
          //让打包目录和我们目录对应
          preserveModules: true,
          exports: 'named',
          //配置打包根目录
          dir: './dist/es',
          globals: { vue: 'Vue' },
        },
        {
          //打包格式
          format: 'cjs',
          //打包后文件名
          entryFileNames: '[name].js',
          //让打包目录和我们目录对应
          preserveModules: true,
          exports: 'named',
          //配置打包根目录
          dir: './dist/lib',
          globals: { vue: 'Vue' },
        },
      ],
    },
  },
})
